﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem
{
    /// <summary>
    /// CommonPage.xaml 的交互逻辑
    /// </summary>
    public partial class CommonPage : Page
    {

        #region Variables

        private string pageName = "";
        private Key downKey;

        #endregion

        #region Constructor

        public CommonPage(MenuListModel menu)
        {
            InitializeComponent();
            this.DataContext = CommonPageViewModel.Instance;
            _userControl.Content = new CommonPageContent(menu);
            MongoDBUtil.Instance.ConnectionChanged += Instance_ConnectionChanged;
            PreviewKeyUp += CommonPage_PreviewKeyUp;
            PreviewKeyDown += CommonPage_PreviewKeyDown;
            PreviewMouseWheel += CommonPage_PreviewMouseWheel;
            this.Loaded += CommonPage_Loaded;
        }

        #endregion

        #region Properties

        public string PageName
        {
            get
            {
                return pageName;
            }

            set
            {
                pageName = value;
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void InitialContent()
        {

        }

        private void Instance_ConnectionChanged(object sender, MongoConnectionArgs e)
        {
            if (!e.ConnectionStatus)
            {
                if (CommonPageViewModel.Instance.WaitCtrlVisibility != Visibility.Visible)
                {
                    CommonPageViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
                }
                CommonPageViewModel.Instance.IsShowTimeOutDialog = true;
                CommonPageViewModel.Instance.WaitMessage = "服务器连接断开，正在重新连接服务器，请稍后...";
            }
            else
            {
                if (CommonPageViewModel.Instance.WaitCtrlVisibility != Visibility.Collapsed && CommonPageViewModel.Instance.IsShowTimeOutDialog)
                {
                    CommonPageViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    CommonPageViewModel.Instance.IsShowTimeOutDialog = false;
                }
            }
        }

        #endregion

        #region Public/Protected Methods

        public void ChangeContent(MenuListModel menu)
        {
            (_userControl.Content as CommonPageContent).InitialContent(menu);
        }

        #endregion

        #region EventHandlers

        private void CommonPage_Loaded(object sender, RoutedEventArgs e)
        {

        }


        private void CommonPage_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            downKey = Key.None;
        }

        private void CommonPage_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (PrintTemplate.PrintVisul == null) return;
            if (PrintTemplate.PrintVisul != null && downKey == Key.LeftCtrl)
            {
                if (e.Delta < 0)
                {
                    PrintTemplate.PrintVisul.ScaleView(false);
                }
                else
                {
                    PrintTemplate.PrintVisul.ScaleView(true);
                }
            }
        }

        private void CommonPage_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (DataManager.Instance.CurrentControl == null) return;
                if (e.Key == Key.LeftCtrl)
                {
                    downKey = e.Key;
                }
                if (e.Key == Key.Enter)
                {
                    if (!HotKeyManger.Instance.IsDropDownOpen)
                    {
                        TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

                        UIElement focusElement = Keyboard.FocusedElement as UIElement;
                        if (focusElement != null)
                        {
                            focusElement.MoveFocus(request);
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        #endregion


    }
}
